import { Component } from "@angular/core";
import {
  NavController,
  NavParams,
  AlertController,
  ActionSheetController,
  ToastController
} from "ionic-angular";
import { HttpService } from "../../../providers/http.service";
import { CommonFunc } from "../../../providers/commonFunc";
import { Storage } from "@ionic/storage";
import { SafeControl } from "../safe-control/bean/safe-control.bean";
import { appConfig } from "../../../providers/config";
import { PhotoViewer } from "@ionic-native/photo-viewer";
import * as moment from "moment";
import { HomePage } from "../../home/home";
import { ElevatorUsedInspectionPage } from "../elevator-used-inspection/elevator-used-inspection";
import { SecurityManagePage } from "../security-manage/security-manage";
import { RecordCheckDtPage } from "../record-check-detail/record-check-detail";
/**
 * 单位安全管理检查明细
 */
@Component({
  templateUrl: "safe-control-detail.html"
})
export class SafeControlDetailPage {
  private safeControl: SafeControl = new SafeControl();
  appConfig = appConfig;
  userId: any;
  userName: any;
  partyId: any;
  partyOtion: any = [];
  isLook = false;
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private httpService: HttpService,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private storage: Storage,
    private photoViewer: PhotoViewer,
    private actionSheet: ActionSheetController,
    private toast: ToastController
  ) {
    this.storage.get("user").then(data => {
      this.userId = data.id;
      this.userName = data.userName;
    });

    //查询
    this.httpService
      .getSafeConById(this.navParams.get("safeConId"))
      .subscribe(data => {
        if (data.status == "200") {
          this.safeControl = data.data;
          if (
            this.safeControl.advicerNameUrl != null &&
            this.safeControl.advicerNameUrl != "null" &&
            this.safeControl.advicerNameUrl != ""
          ) {
            this.isLook = true;
          }
          this.safeControl.questionsUrls = [];
          if (this.safeControl.questionsUrl) {
            this.safeControl.questionsUrls = this.safeControl.lawDocs.split(
              ","
            );
          }
          this.safeControl.checkTimeTo = this.commonFunc.transformtime(
            this.safeControl.checkTimeTo
          );
        } else {
          let tt = this.toast.create({
            message: "信息查询失败，请重试",
            duration: 2000,
            position: "top"
          });
          tt.present();
        }
      });
  }

  submit() {
    let param = {
      adviceTime: moment(new Date()).format("YYYYMMDDHHmmss"),
      advicerNameUrl: this.safeControl.advicerNameUrl,
      advicerName: this.userName,
      advicerId: this.userId,
      advice: this.safeControl.advice,
      id: this.safeControl.id,
      usedPartyId: this.safeControl.usedPartyId,
      checkerId: this.safeControl.checkerId,
      isSure: "1"
    };

    this.httpService.updateSafeContr(param).subscribe(data1 => {
      if (data1.status == 200) {
        this.httpService
          .updateReadState({ id: this.navParams.get("noticeId"), isRead: "1" })
          .subscribe(data => {
            if (data.status == 200) {
              let tt = this.toast.create({
                message: "提交成功",
                duration: 2000,
                position: "top"
              });
              tt.present().then(() => {
                this.navCtrl.setRoot(HomePage).then(() => {
                  window["jQuery"]("#signatureid").remove();
                  window["jQuery"]("#showImage").remove();
                  this.navCtrl
                    .getActive()
                    .getNav()
                    .remove(0);
                });
              });
            } else {
              const alert1 = this.alertCtrl.create({
                subTitle: "更新消息状态失败!",
                buttons: ["确定"]
              });
              alert1.present();
            }
          });
      } else {
        const alert = this.alertCtrl.create({
          subTitle: "信息提交失败",
          buttons: ["确定"]
        });
        alert.present();
      }
    });
  }

  onHold(param) {
    let actionSheet = this.actionSheet.create({
      buttons: [
        {
          text: "查看大图",
          role: "查看大图",
          handler: () => {
            let url: string = param;
            this.photoViewer.show(
              encodeURI(
                this.appConfig.IP_URL + this.appConfig.API.getUpload + url
              ).replace("%5C", "/"),
              null,
              { share: false }
            );
          }
        },
        {
          text: "取消",
          role: "取消",
          handler: () => {
            //console.log('Cancel clicked');
          }
        }
      ]
    });
    actionSheet.present();
  }

  detailCheck() {
    this.navCtrl.push(RecordCheckDtPage, {
      id: this.safeControl.id,
      isadd: false
    });
  }

  getImgUrl(param) {
    this.safeControl.advicerNameUrl = param.imageDate;
  }
}
